package models;


import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.hibernate.annotations.ManyToAny;

import play.db.jpa.Model;

@Entity
@SuppressWarnings("deprecation")

public class Teacher extends Model	{
	
	public String name;
	public String address;
	public String homePhone;
	
	@OneToMany(mappedBy="teacher" , cascade=CascadeType.ALL)
	public List<Salary> salaries;
	
	public String email1;
	
	public String email2;
	
	public String mobilePhone1;
	
	public String mobilePhone2;
	
	public float percentage;
	
	@ManyToMany
	public List<Activity> activities;
	
	@OneToMany(mappedBy="teacher", cascade=CascadeType.ALL)
	public List<Register> registrations;
	
	
	public Teacher(String name, String address, String homePhone,
			String mobilePhone1, String email1) {
		
		this.name = name;
		this.address = address;
		this.homePhone = homePhone;
		this.mobilePhone1 = mobilePhone1;
		this.email1 = email1;
		activities = new LinkedList<Activity>();
		registrations = new LinkedList<Register>();
		salaries = new LinkedList<Salary>();
	}
	
	public LinkedList<Class> getAttendedClasses(int month) {
		
		List<Register> regs = registrations;
		LinkedList<Class> classes = new LinkedList<Class>();
		
		
		for(int i=0 ; i<regs.size() ; i++) {
			List<Class> cs = regs.get(i).sClass;
			
			for(int j=0 ; j<cs.size() ; j++) {
				Class c = cs.get(j);
				if(c.teacher_attendance == Class.ATTENDED 
					&& c.datetime.getMonth() == month) {
					classes.add(c);
					
				}
			}
		}
		return classes;
		
	}
	
	public LinkedList<Class> getClasses(int month) {
		
		List<Register> regs = registrations;
		LinkedList<Class> classes = new LinkedList<Class>();
		
		
		for(int i=0 ; i<regs.size() ; i++) {
			
			classes.addAll(regs.get(i).sClass);			
		}
		
		return classes;
		
	}
	
	public int calculateThisMonthSalary() {
		
		int thisMonth = new Date().getMonth();
		
		int amount = (int) Math.ceil(percentage*getAttendedClasses(thisMonth).size());
		
		return amount;
		
	}
	
	public Salary setSalary(int amount) {
		
		Date d = new Date();
		
		int thisMonth = d.getMonth();
		int thisYear = d.getYear();
		
		Salary s = new Salary(amount, thisMonth, thisYear, this);
		int n = salaries.size()-1;
		
		if(salaries.size()>0) {
			if(salaries.get(n).month != thisMonth
				|| salaries.get(n).year != thisYear) {
				
				salaries.add(s);
			}
			else {
				salaries.get(n).amount = amount;
				s = salaries.get(n);
			}
		}
		else {
			salaries.add(s);
		}
			return s;
		
	}
	
	
	
	
	
	
	
	
	

}
